//
//  MyScrollViewController.swift
//  viewcontroller_test
//
//  Created by 潘茹燕 on 16/4/1.
//  Copyright © 2016年 海南开工信息技术有限公司. All rights reserved.
//

import UIKit

class MyScrollViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!
    
    
    @IBAction func onClickDoSomething(sender: AnyObject) {
        
        NSLog("\(scrollView.contentSize)")
        NSLog("\(scrollView.contentOffset)")
        NSLog("\(scrollView.contentInset)")
        
        UIView.animateWithDuration(0.3, animations: {
            self.scrollView.contentSize.height = 800
        }){ _ in
            
            self.scrollView.scrollEnabled = true
    
        }
        
        
    }
    
    
    
    var view_top: UIView?
    var view_bottom: UIView?
    var view_Indicator: UILabel?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        
        view_top = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 800))
        
        
        view_top!.backgroundColor = UIColor.grayColor()
        
        scrollView.addSubview(view_top!)
        
        view_Indicator = UILabel(frame: CGRect(x: 0, y: 800, width: 320, height: 40))
        view_Indicator?.text = "继续拖动，查看图文详情"
        view_Indicator?.font = UIFont.systemFontOfSize(11)
        view_Indicator?.textAlignment = .Center
        scrollView.addSubview(view_Indicator!)
        
        view_bottom = UIView(frame: CGRect(x: 0, y: 840, width: 320, height: 800))
        view_bottom!.backgroundColor = UIColor.darkGrayColor()
        scrollView.addSubview(view_bottom!)
        
        
        scrollView.contentSize = CGSize(width: 320, height: 840)
        
        scrollView.delegate = self
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    // helper function to calculate how far we can scroll
    func calcScrollRange() -> CGFloat {
        
        let delta = scrollView.contentSize.height - (scrollView.frame.height - scrollView.contentInset.top - scrollView.contentInset.bottom)
        
        return max(0.0, delta)
    }
    
    
    var canTransition = false
    var transiting = false
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //NSLog("scrollView did scroll(contentOffset):\(scrollView.contentOffset)")
        
        let scrollRange = calcScrollRange()
        
        let delta = scrollView.contentOffset.y - scrollView.contentInset.top - scrollRange
        
        NSLog("dragging:\(scrollView.dragging), tracking:\(scrollView.tracking), decelart:\(scrollView.decelerating)")
        
        if transiting {
            
            NSLog("Transiting")
            
        }
        else {
        
        
            if canTransition && scrollView.decelerating {
                
                beginTransition()
            }
            
            
            else if delta > 100.0 && !scrollView.decelerating {
                
                view_Indicator!.text = "释放拖动，查看图文详情"
                
                canTransition = true
                NSLog("can")

            }
            else {
                view_Indicator?.text = "继续拖动，查看图文详情"
                canTransition = false
            }
        }
    }
    
    func beginTransition() {
        transiting = true
        
        scrollView.scrollEnabled = false
        scrollView.contentSize = CGSize(width: 320, height: 1640)
        
        
        
        UIView.animateWithDuration(0.3, animations: {
            self.scrollView.contentOffset.y = 840
            }) { (_) in
                self.endTransition()
        }
        
        
    }
    
    func endTransition() {
        
        transiting = false
        canTransition = false
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
